/************************************************/
/*creator: johan candelaria						*/
/*project name: hello_world1					*/
/*notes: dit programma moet een ledje laten		*/
/*		 blinken : D							*/
/*												*/
/*contact: pinkpanter127@hotmail.com			*/
/*												*/
/*hardware: 	pic 18f4550						*/
/*				sw1 d1							*/
/*				led1= d2						*/
/*												*/
/*												*/
/*												*/
/*												*/
/************************************************/
	/*includes*/

#include <p18f4550.h>
#include <usart.h>
#include <delays.h>

#define AAN = 1
#define UIT = 0

unsigned int c1_alarm = 0;
unsigned int c2_alarm = 0;

	/*defines*/
#define Fcy  48000000	/* we draaien op ((20mhz/5)*24(=96))/2=48mhz */
	
	/* Set up the configuration bits */
	#pragma config WDT = OFF, BOR = OFF, MCLRE = ON, PWRT = ON, LVP = OFF , CCP2MX = OFF	//gaat goed
	#pragma config PLLDIV = 5, CPUDIV = OSC1_PLL2, USBDIV = 2, FOSC = HSPLL_HS//cpu=hs, ppl enabled+ppl=/4+postscaler=/2. freq=48mhz

	int i, flippie= 0;
	int brghc;
	char TempStr[16]; 
	unsigned char usartbuff[16];




	void longdelay(void){
		for (i=1; i<=4;i++){
		Delay10KTCYx(250);	// delay 10*250 cycles
		}
	}

	void init_pins(void){
		TRISA = 0b11111111;	/* configure PORTA for input */
		TRISB = 0b00100001; //rb0&7=input76543210=10000001
		TRISC = 0x00;
		TRISD = 0b00000010;	//rd1=input
		TRISE = 0b11100000; //re0-2=input
		ADCON1 = 0b00000000; //All ADC disabled 0101 wil niet werken
		LATDbits.LATD2 = 0; //ledje is active low
	}	
	
	void init_rs232(unsigned int snelheid){
		/* Open the USART configured as 8N1, 9600 baud, in polled mode */
  			brghc= (((48000000/115200)/16)-1);	//57600
			OpenUSART( USART_TX_INT_OFF &
			USART_RX_INT_OFF &
			USART_ASYNCH_MODE &
			USART_EIGHT_BIT &
			USART_CONT_RX &
			USART_BRGH_HIGH,
			brghc );
			putrsUSART ("AAN");
			}

//----------------------------------------------------------------------------
// Low priority interrupt routine

#pragma code

#pragma code _HIGH_INTERRUPT_VECTOR = 0x0808
#pragma code _LOW_INTERRUPT_VECTOR = 0x0818

#pragma code

/* 
Interrupt */

//_high_ISR */

void _high_ISR (void){
}

//_low_ISR */

void _low_ISR (void)
{
      putrsUSART ("interrupt occured");		//test
  		if (PIR2bits.CMIF){                 //check comnparator interupt flag
      		PIR2bits.CMIF = 0;            	//clear interrupt flag
      		//Flags.Bit.Timeout = 1;            //indicate timeout
      		putrsUSART ("comparator interupted");
	  		LATD ^= (1 << 2);
    		}//end if

}



//----------------------------------------------------------------------------


void main (void)
	{
		init_pins();
		init_rs232(115200);
		RCONbits.IPEN = 1; 		//Enable priority levels on interrupts
		RCONbits.SBOREN = 0;	//Disable BOR, Brown-out Reset enabled in hardware only
		PIE2bits.CMIE=1;		//comparator interupt enable bit
		INTCONbits.PEIE=1;		//if IPEN=1: enables all low priority interupts
		INTCONbits.GIE=1;		//set global interupts	
		IPR2bits.CMIP=0;		//comparator priority=low
		PIE2bits.CMIE=1;		//Comparator Interrupt Enable bit	
		CMCON=0b00000010;		//configure comparators bits

		


		while(1){
						if(PORTBbits.RB5==0){
							while(PORTBbits.RB5==0){
							}
							putrsUSART ("alive");
							LATD ^= (1 << 2);	
							//longdelay();
						}//close if
						else if (CMCONbits.C1OUT==1){
						putrsUSART ("c1 is laag geweest");
						c1_alarm=1;
							while (c1_alarm == 1) {
							Delay1KTCYx(50);	// delay 10*250 cycles
							LATD ^= (1 << 2);
								if(PORTBbits.RB5==0){
									while(PORTBbits.RB5==0){
									}
								c1_alarm=0;
								}
							}
						}//close else if
						else if (CMCONbits.C2OUT==1){
						putrsUSART ("c2 is laag geweest");
						c2_alarm=1;
							while (c2_alarm == 1) {
							Delay1KTCYx(10);	// delay 10*250 cycles
							LATD ^= (1 << 2);
								if(PORTBbits.RB5==0){
									while(PORTBbits.RB5==0){
									}
								c2_alarm=0;
								}
							}
						}//close else if	
							
		}// close while(1)
		
		//Delay1KTCYx(10);	// delay 1*10 cycles
		
	}